local RoomBase = require 'packages.mahjong.riichi_mahjong.core.roombase'
local GameLogic = require "packages.mahjong.riichi_mahjong.server.gamelogic"
local ServerPlayer = require "packages.mahjong.riichi_mahjong.server.serverplayer"
local TurnData = require "packages.mahjong.riichi_mahjong.core.turn_data"

---@class RiichiMahjong.Room : RiichiMahjong.RoomBase, ServerRoomBase
local Room = RoomBase:subclass("RiichiMahjong.Room")
Room:include(Fk.Base.ServerRoomBase)

-- 手写泛型 这里帮你写了

---@class RiichiMahjong.Room
---@field public players RiichiMahjong.ServerPlayer[]
---@field public observers RiichiMahjong.ServerPlayer[]
---@field public current RiichiMahjong.ServerPlayer
---@field public turn_num integer @ 当前轮数
---@field public card_pile integer[] @ 牌山
---@field public dead_wall integer[] @ 王牌（宝牌指示牌5|里宝牌5|岭上牌4）
---@field public records RiichiMahjong.TurnData[] @ 记录
---@field public finished boolean @ 是否结束了
---@field public turn_data RiichiMahjong.TurnData @ 本轮数据
---@field public logic RiichiMahjong.GameLogic
---@field public getPlayerById fun(self: RiichiMahjong.Room, id: integer): RiichiMahjong.ServerPlayer
---@field public getPlayerBySeat fun(self: RiichiMahjong.Room, seat: integer): RiichiMahjong.ServerPlayer
---@field public setCurrent fun(self: RiichiMahjong.Room, current: RiichiMahjong.ServerPlayer)
---@field public getCurrent fun(self: RiichiMahjong.Room): RiichiMahjong.ServerPlayer

function Room:initialize(_room)
  RoomBase.initialize(self)
  Fk.Base.ServerRoomBase.initialize(self, _room)

  self.card_pile = {}
  self.dead_wall = {}
  self.records = {}

  self.serverplayer_klass = ServerPlayer
  self.logic_klass = GameLogic
end
---摸牌
---@param tile integer
---@param player RiichiMahjong.ServerPlayer
function Room:DrawTile(tile, player)
  table.insert(player.player_tiles, tile)
  player.card_num = player.card_num + 1
  for _, p in ipairs(self.players) do
    self:doBroadcastNotify("RiichiMahjong.DrawTile", { t = (p == player and tile or -1), p = player.id}, {p})
  end
  
end

function Room:setup()
  local room = self
  for _, p in ipairs(self.players) do
    self:doBroadcastNotify("RiichiMahjong.SetUp", {t = p.player_tiles, d = room.dead_wall[1]}, {p})
  end
end

function Room:getLastTurnData()
  return self.records[#self.records]
end

function Room:turnEnd()
  table.insert(self.records, self.turn_data)
  self.turn_data = TurnData:new()
end

function Room:newTurn()
  self.turn_num = self.turn_num + 1
end

function Room:newData()
  return self.records[self.turn_num]
end

function Room:win ()
  self.finished = true
end

function Room:Ryukyoku()
  self.finished = true
end

---让player将一部分牌副露（包括暗杠）
---@param player RiichiMahjong.ServerPlayer
---@param meld RiichiMahjong.Meld
function Room:call(player, meld)
  self.first_round = false
  for _, tile in ipairs(meld.tiles) do
    table.removeOne(player.player_tiles, tile)
  end
  table.insert(player.melds, meld)
  self:doBroadcastNotify("RiichiMahjong.Call", {p = player.id, m = meld})
end

---某player弃牌
---@param player RiichiMahjong.ServerPlayer
---@param tileId integer
function Room:discard(player, tileId)
  table.removeOne(player.player_tiles, tileId)
  self:doBroadcastNotify("RiichiMahjong.Discard", {p = player.id, t = tileId})
end

return Room
